home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-admin / users.php < prev    next >
Encoding:
PHP Script  |  2005-03-26  |  11.1 KB  |  332 lines

  1. <?php
  2. require_once('admin.php');
  3.  
  4. $title = __('Users');
  5. $parent_file = 'profile.php';
  6.     
  7. $wpvarstoreset = array('action');
  8. for ($i=0; $i<count($wpvarstoreset); $i += 1) {
  9.     $wpvar = $wpvarstoreset[$i];
  10.     if (!isset($$wpvar)) {
  11.         if (empty($_POST["$wpvar"])) {
  12.             if (empty($_GET["$wpvar"])) {
  13.                 $$wpvar = '';
  14.             } else {
  15.                 $$wpvar = $_GET["$wpvar"];
  16.             }
  17.         } else {
  18.             $$wpvar = $_POST["$wpvar"];
  19.         }
  20.     }
  21. }
  22.  
  23. switch ($action) {
  24. case 'adduser':
  25.     check_admin_referer();
  26.  
  27.     $user_login     = wp_specialchars(trim($_POST['user_login']));
  28.     $pass1          = $_POST['pass1'];
  29.     $pass2          = $_POST['pass2'];
  30.     $user_email     = wp_specialchars(trim($_POST['email']));
  31.     $user_firstname = wp_specialchars(trim($_POST['firstname']));
  32.     $user_lastname  = wp_specialchars(trim($_POST['lastname']));
  33.     $user_uri       = wp_specialchars(trim($_POST['uri']));
  34.         
  35.     /* checking that username has been typed */
  36.     if ($user_login == '')
  37.         die (__('<strong>ERROR</strong>: Please enter a username.'));
  38.  
  39.     /* checking the password has been typed twice */
  40.     do_action('check_passwords', array($user_login, &$pass1, &$pass2));
  41.     if ($pass1 == '' || $pass2 == '')
  42.         die (__('<strong>ERROR</strong>: Please enter your password twice.'));
  43.  
  44.     /* checking the password has been typed twice the same */
  45.     if ($pass1 != $pass2)
  46.         die (__('<strong>ERROR</strong>: Please type the same password in the two password fields.'));
  47.  
  48.     $user_nickname = $user_login;
  49.  
  50.     /* checking that the username isn't already used by another user */
  51.     $loginthere = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE user_login = '$user_login'");
  52.     if ($loginthere)
  53.         die (__('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
  54.  
  55.     /* checking e-mail address */
  56.     if (empty($_POST["email"])) {
  57.         die (__("<strong>ERROR</strong>: please type an e-mail address"));
  58.         return false;
  59.     } else if (!is_email($_POST["email"])) {
  60.         die (__("<strong>ERROR</strong>: the email address isn't correct"));
  61.         return false;
  62.     }
  63.  
  64.     $user_ID = $wpdb->get_var("SELECT ID FROM $wpdb->users ORDER BY ID DESC LIMIT 1") + 1;
  65.  
  66.     $user_nicename = sanitize_title($user_nickname, $user_ID);
  67.     $user_uri = preg_match('/^(https?|ftps?|mailto|news|gopher):/is', $user_uri) ? $user_uri : 'http://' . $user_uri;
  68.     $now = gmdate('Y-m-d H:i:s');
  69.     $new_users_can_blog = get_settings('new_users_can_blog');
  70.  
  71.     $result = $wpdb->query("INSERT INTO $wpdb->users 
  72.         (user_login, user_pass, user_nickname, user_email, user_ip, user_domain, user_browser, user_registered, user_level, user_idmode, user_firstname, user_lastname, user_nicename, user_url)
  73.     VALUES 
  74.         ('$user_login', MD5('$pass1'), '$user_nickname', '$user_email', '$user_ip', '$user_domain', '$user_browser', '$now', '$new_users_can_blog', 'nickname', '$user_firstname', '$user_lastname', '$user_nicename', '$user_uri')");
  75.     
  76.     if ($result == false)
  77.         die (__('<strong>ERROR</strong>: Couldn’t register you!'));
  78.  
  79.     $stars = '';
  80.     for ($i = 0; $i < strlen($pass1); $i = $i + 1)
  81.         $stars .= '*';
  82.  
  83.     $user_login = stripslashes($user_login);
  84.     $message  = sprintf(__('New user registration on your blog %s:'), get_settings('blogname')) . "\r\n\r\n";
  85.     $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
  86.     $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
  87.  
  88.     @wp_mail(get_settings('admin_email'), sprintf(__('[%s] New User Registration'), get_settings('blogname')), $message);
  89.     header('Location: users.php');
  90. break;
  91.  
  92. case 'promote':
  93.     check_admin_referer();
  94.  
  95.     if (empty($_GET['prom'])) {
  96.         header('Location: users.php');
  97.     }
  98.  
  99.     $id = $_GET['id'];
  100.     $prom = $_GET['prom'];
  101.  
  102.     $user_data = get_userdata($id);
  103.     $usertopromote_level = $user_data->user_level;
  104.  
  105.     if ($user_level <= $usertopromote_level) {
  106.         die(__('Can’t change the level of a user whose level is higher than yours.'));
  107.     }
  108.  
  109.     if ('up' == $prom) {
  110.         $new_level = $usertopromote_level + 1;
  111.         $sql="UPDATE $wpdb->users SET user_level=$new_level WHERE ID = $id AND $new_level < $user_level";
  112.     } elseif ('down' == $prom) {
  113.         $new_level = $usertopromote_level - 1;
  114.         $sql="UPDATE $wpdb->users SET user_level=$new_level WHERE ID = $id AND $new_level < $user_level";
  115.     }
  116.     $result = $wpdb->query($sql);
  117.  
  118.     header('Location: users.php');
  119.  
  120. break;
  121.  
  122. case 'delete':
  123.  
  124.     check_admin_referer();
  125.  
  126.     $id = (int) $_GET['id'];
  127.  
  128.     if (!$id) {
  129.         header('Location: users.php');
  130.     }
  131.  
  132.     $user_data = get_userdata($id);
  133.     $usertodelete_level = $user_data->user_level;
  134.  
  135.     if ($user_level <= $usertodelete_level)
  136.         die(__('Can’t delete a user whose level is higher than yours.'));
  137.  
  138.     $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
  139.     if ($post_ids) {
  140.         $post_ids = implode(',', $post_ids);
  141.         
  142.         // Delete comments, *backs
  143.         $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($post_ids)");
  144.         // Clean cats
  145.         $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id IN ($post_ids)");
  146.         // Clean post_meta
  147.         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id IN ($post_ids)");
  148.         // Clean links
  149.         $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
  150.         // Delete posts
  151.         $wpdb->query("DELETE FROM $wpdb->posts WHERE post_author = $id");
  152.     }
  153.  
  154.     // FINALLY, delete user
  155.     $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
  156.     header('Location: users.php?deleted=true');
  157.  
  158. break;
  159.  
  160. default:
  161.     
  162.     include ('admin-header.php');
  163.     ?>
  164.  
  165. <?php if (isset($_GET['deleted'])) : ?>
  166. <div class="updated"><p><?php _e('User deleted.') ?></p></div>
  167. <?php endif; ?>
  168. <div class="wrap">
  169.   <h2><?php _e('Authors') ?></h2>
  170.   <table cellpadding="3" cellspacing="3" width="100%">
  171.     <tr>
  172.     <th><?php _e('ID') ?></th>
  173.     <th><?php _e('Nickname') ?></th>
  174.     <th><?php _e('Name') ?></th>
  175.     <th><?php _e('E-mail') ?></th>
  176.     <th><?php _e('Website') ?></th>
  177.     <th><?php _e('Level') ?></th>
  178.     <th><?php _e('Posts') ?></th>
  179.     <th> </th>
  180.     </tr>
  181.     <?php
  182.     $users = $wpdb->get_results("SELECT ID FROM $wpdb->users WHERE user_level > 0 ORDER BY ID");
  183.     $style = '';
  184.     foreach ($users as $user) {
  185.         $user_data = get_userdata($user->ID);
  186.         $email = $user_data->user_email;
  187.         $url = $user_data->user_url;
  188.         $short_url = str_replace('http://', '', $url);
  189.         $short_url = str_replace('www.', '', $short_url);
  190.         if ('/' == substr($short_url, -1))
  191.             $short_url = substr($short_url, 0, -1);
  192.         if (strlen($short_url) > 35)
  193.         $short_url =  substr($short_url, 0, 32).'...';
  194.         $style = ('class="alternate"' == $style) ? '' : 'class="alternate"';
  195.         $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $user->ID and post_status = 'publish'");
  196.         if (0 < $numposts) $numposts = "<a href='edit.php?author=$user_data->ID' title='" . __('View posts') . "'>$numposts</a>";
  197.         echo "
  198. <tr $style>
  199.     <td align='center'>$user_data->ID</td>
  200.     <td><strong>$user_data->user_nickname</strong></td>
  201.     <td>$user_data->user_firstname $user_data->user_lastname</td>
  202.     <td><a href='mailto:$email' title='" . sprintf(__('e-mail: %s'), $email) . "'>$email</a></td>
  203.     <td><a href='$url' title='website: $url'>$short_url</a></td>
  204.     <td align='center'>";
  205.     if (($user_level >= 2) and ($user_level > $user_data->user_level) and ($user_data->user_level > 0))
  206.         echo " <a href=\"users.php?action=promote&id=".$user_data->ID."&prom=down\">-</a> ";
  207.     echo $user_data->user_level;
  208.     if (($user_level >= 2) and ($user_level > ($user_data->user_level + 1)))
  209.         echo " <a href=\"users.php?action=promote&id=".$user_data->ID."&prom=up\">+</a> ";
  210.     echo "</td><td align='right'>$numposts</td>";
  211.     echo '<td>';
  212.     if (($user_level >= 2) and ($user_level > $user_data->user_level))
  213.         echo "<a href='user-edit.php?user_id=$user_data->ID' class='edit'>".__('Edit')."</a>";
  214.     echo '</td>';
  215.     echo '</tr>';
  216.     }
  217.     
  218.     ?>
  219.     
  220.   </table>
  221. </div>
  222.  
  223. <?php
  224. $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_level = 0 ORDER BY ID");
  225. if ($users) {
  226. ?>
  227. <div class="wrap">
  228.     <h2><?php _e('Registered Users') ?></h2>
  229.     <table cellpadding="3" cellspacing="3" width="100%">
  230.     <tr>
  231.         <th><?php _e('ID') ?></th>
  232.         <th><?php _e('Nickname') ?></th>
  233.         <th><?php _e('Name') ?></th>
  234.         <th><?php _e('E-mail') ?></th>
  235.         <th><?php _e('Website') ?></th>
  236.         <th></th>
  237.         <th></th>
  238.         <th></th>
  239.     </tr>
  240. <?php
  241. $style = '';
  242. foreach ($users as $user) {
  243.     $user_data = get_userdata($user->ID);
  244.     $email = $user_data->user_email;
  245.     $url = $user_data->user_url;
  246.     $short_url = str_replace('http://', '', $url);
  247.     $short_url = str_replace('www.', '', $short_url);
  248.     if ('/' == substr($short_url, -1))
  249.         $short_url = substr($short_url, 0, -1);
  250.     if (strlen($short_url) > 35)
  251.     $short_url =  substr($short_url, 0, 32).'...';
  252.     $style = ('class="alternate"' == $style) ? '' : 'class="alternate"';
  253. echo "\n<tr $style>
  254. <td align='center'>$user_data->ID</td>
  255. <td><strong>$user_data->user_nickname</strong></td>
  256. <td>$user_data->user_firstname $user_data->user_lastname</td>
  257. <td><a href='mailto:$email' title='" . sprintf(__('e-mail: %s'), $email) . "'>$email</a></td>
  258. <td><a href='$url' title='website: $url'>$short_url</a></td>
  259. <td align='center'>";
  260.  
  261.     if ($user_level >= 6)
  262.         echo "<a href='users.php?action=promote&id=$user_data->ID&prom=up' class='edit'>". __('Promote') . '</a>';    
  263.     echo "</td>\n";
  264.     echo '<td>';
  265.     if (($user_level >= 6) and ($user_level > $user_data->user_level))
  266.         echo "<a href='user-edit.php?user_id=$user_data->ID' class='edit'>".__('Edit')."</a>";
  267.     echo '</td><td>';
  268.     if ($user_level >= 6)
  269.         echo "<a href='users.php?action=delete&id=$user_data->ID' class='delete' onclick='return confirm(\"" . __('You are about to delete this user \n  OK to delete, Cancel to stop.') . "\")'>" . __('Delete'). '</a>';
  270.     echo '</td></tr>';
  271.  
  272. }
  273.  
  274. ?>
  275.     
  276.     </table>
  277.       <p><?php _e('Deleting a user also deletes all posts made by that user.') ?></p>
  278. </div>
  279.  
  280.     <?php 
  281.     } ?>
  282. <div class="wrap">
  283. <h2><?php _e('Add New User') ?></h2>
  284. <?php printf(__('<p>Users can <a href="%s/wp-register.php">register themselves</a> or you can manually create users here.</p>'), get_settings('siteurl')); ?>
  285. <form action="" method="post" name="adduser" id="adduser">
  286.   <table class="editform" width="100%" cellspacing="2" cellpadding="5">
  287.     <tr>
  288.       <th scope="row" width="33%"><?php _e('Nickname') ?>
  289.       <input name="action" type="hidden" id="action" value="adduser" /></th>
  290.       <td width="66%"><input name="user_login" type="text" id="user_login" /></td>
  291.     </tr>
  292.     <tr>
  293.       <th scope="row"><?php _e('First Name') ?> </th>
  294.       <td><input name="firstname" type="text" id="firstname" /></td>
  295.     </tr>
  296.     <tr>
  297.       <th scope="row"><?php _e('Last Name') ?> </th>
  298.       <td><input name="lastname" type="text" id="lastname" /></td>
  299.     </tr>
  300.     <tr>
  301.       <th scope="row"><?php _e('E-mail') ?></th>
  302.       <td><input name="email" type="text" id="email" /></td>
  303.     </tr>
  304.     <tr>
  305.       <th scope="row"><?php _e('Website') ?></th>
  306.       <td><input name="uri" type="text" id="uri" /></td>
  307.     </tr>
  308. <?php
  309. $show_password_fields = apply_filters('show_password_fields', true);
  310. if ( $show_password_fields ) :
  311. ?>
  312.     <tr>
  313.       <th scope="row"><?php _e('Password (twice)') ?> </th>
  314.       <td><input name="pass1" type="password" id="pass1" />
  315.       <br />
  316.       <input name="pass2" type="password" id="pass2" /></td>
  317.     </tr>
  318. <?php endif; ?>
  319.   </table>
  320.   <p class="submit">
  321.     <input name="adduser" type="submit" id="adduser" value="<?php _e('Add User') ?> »" />
  322.   </p>
  323.   </form>
  324. </div>
  325.     <?php
  326.  
  327. break;
  328. }
  329.  
  330. include('admin-footer.php');
  331. ?>
  332.